import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {Message} from 'element-plus'

const request = axios.create({
    baseURL: 'http://localhost:8000/',
    timeout: 10000
})

// let CancelToken = axios.CancelToken

// 请求拦截
request.interceptors.request.use(config => {
        // 对于访客模式，除GET请求外，都拦截并提示
        // const json = window.localStorage.getItem('user') || '{}'
        // const user = JSON.parse(json)
        // if (json !== '{}' && user.role !== 'ROLE_admin' && config.method !== 'get') {
        //     config.cancelToken = new CancelToken(function executor(cancel) {
        //         cancel('演示模式，不允许操作')
        //     })
        //     return config
        // }

        NProgress.start()
        const token = window.localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = token
        }
        return config
    },
    error => {
        console.info(error)
        return Promise.reject(error)
    }
)

// 响应拦截
request.interceptors.response.use(response => {
        NProgress.done()
        const res = response.data
        // if (res.code !== 200) {
        //     let msg = res.msg || 'Error'
        //     Message.error(msg)
        //     return Promise.reject(new Error(msg))
        // }
        return res
    },
    error => {
        console.log(error)
        Message.error(error)
        return Promise.reject(error)
    }
)

export default request
